// pages/allclass/allclass.js
const db = wx.cloud.database();
const app = getApp();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 一次拿全部的存在这里
    allCourse: [],
    // 用户筛选出来要展示的
    showCourse: [],

    leftDistance: "-1000rpx",
    showGreyCover: false,
    // 当前标签
    currentTag: '全部',
    tags: [{
      tagName: '全部'
    }, {
      tagName: '数字建设'
    }, {
      tagName: '种植业'
    }, {
      tagName: '畜牧业'
    }, {
      tagName: '农林业'
    }, {
      tagName: '建筑业'
    }, {
      tagName: '其他'
    }]
  },

  // -----------------methods----------------
  navToSearch() {
    console.log("搜索");
    wx.navigateTo({
      url: '/pages/searchCourse/searchCourse',
    })
  },
  currentTagChange(e) {
    let that = this;
    console.log(e.currentTarget.dataset.currenttag.tagName);
    let tagName = e.currentTarget.dataset.currenttag.tagName;
    // 为什么要刷新currentTag
    this.setData({
      currentTag: tagName
    })

    if (tagName == "全部") {
      this.setData({
        showCourse: that.data.allCourse
      })
      return;
    }

    // 1、根据currentTag做标签激活态样式(已完成)
    // 2、根据currentTag对showCourse做刷新，showCourse要怎么根据currentTag做改变？
    // 数据结构怎么查询
    let temp = [];
    for (let i = 0; i < this.data.allCourse.length; i++) {
      if (this.data.allCourse[i].tag == tagName) {
        temp.push(this.data.allCourse[i])
      }
    }
    this.setData({
      showCourse: temp
    })
  },
  addSee(e) {
    let that = this;
    console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index;
    let course_id = that.data.allCourse[index]._id;
    db.collection('userCollect').where({
      _openid: app.personInfo._openid,
      course_id: course_id
    }).get({
      success: res => {
        if (res.data.length == 0) {
          //没点过，现在点
          db.collection('userCollect').add({
            data: {
              type: "访问量",
              _openid: app.personInfo._openid,
              course_id: course_id
            },
            success: res => {
              console.log("成功记录访问者");
            }
          })
          db.collection('course').doc(course_id).update({
            data: {
              see: _.inc(1)
            },
            success: res => {
              that.setData({
                ['allCourse[' + index + '].see']: that.data.allCourse[index].see + 1,
              });
              console.log('当前课程的点击量：' + that.data.allCourse[index].see);
            }
          })
        } else {
          console.log("用户已经访问过这个课程了");
        }
      },
      complete() {
        that.transformationcard(e.currentTarget.dataset.correspondinginfo);
      }
    })
  },


  transformationcard(correspondinginfo) {
    getApp().navTmpInfo = correspondinginfo;

    wx.navigateTo({
      url: "/pages/videocard/videocard"
    })
  },
  /*transformationcard(e) {
    // console.log(e);
    console.log(e.currentTarget.dataset.correspondinginfo);

    getApp().navTmpInfo = e.currentTarget.dataset.correspondinginfo;

    wx.navigateTo({
      url: "/pages/videocard/videocard"
    })
  },*/

  getBackclass() {
    wx.navigateBack({
      url: "/pages/agriculturalclass/agriculturalclass",
    })
  },

  showLeftBox() {
    console.log("显示筛选盒子")
    this.setData({
      leftDistance: "0px",
      showGreyCover: true
    });
  },
  hideLeftBox() {
    console.log("挪走筛选盒子")
    this.setData({
      leftDistance: "-1000rpx",
      showGreyCover: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    wx.cloud.callFunction({
      name: "getAllRecord",
      data: {
        collectionName: "course"
      },
      success: function (res) {
        console.log(res.result.all);
        that.setData({
          allCourse: res.result.all
        });
        that.setData({
          showCourse: res.result.all
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})